<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入学号或学生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="auditLeaveList" @selection-change="handleSelectionChange">
      <el-table-column label="申请人" align="center" prop="stuName" />
      <el-table-column label="学号" align="center" prop="sno" />
      <el-table-column label="假期类别" align="center" prop="leaveType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.parentLevelId != null" type="warning">延假</el-tag>
          <el-tag v-else>请假</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="请假类型" align="center" prop="leaveType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.leave_types" :value="scope.row.leaveType" />
        </template>
      </el-table-column>
      <el-table-column label="天数" align="center" prop="duration">
        <template slot-scope="scope">
          <span>{{ scope.row.duration / 10 + '天' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.leave_audit" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleXq(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="xqOpen" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div v-if="form.parentLevelId == null">
          <el-descriptions title="" direction="vertical" :column="5" border>
            <el-descriptions-item label="申请人" :span="1">{{ form.stuName }}</el-descriptions-item>
            <el-descriptions-item label="学号" :span="1">{{ form.sno }}</el-descriptions-item>
            <el-descriptions-item label="假期类别">
              <el-tag v-if="form.parentLevelId != null" type="warning">延假</el-tag>
              <el-tag v-else>请假</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="请假类型">
              <dict-tag :options="dict.type.leave_types" :value="form.leaveType" />
            </el-descriptions-item>
            <el-descriptions-item label="审核状态">
              <dict-tag :options="dict.type.leave_audit" :value="form.newStatus" />
            </el-descriptions-item>
            <el-descriptions-item label="审核人" :span="1">{{ form.auditName }}</el-descriptions-item>
            <el-descriptions-item label="累计天数" :span="1">{{ form.duration / 10 }}天</el-descriptions-item>
            <el-descriptions-item label="开始时间" :span="2">{{ form.beginTime }}</el-descriptions-item>
            <el-descriptions-item label="结束时间" :span="2">{{ form.endTime }}</el-descriptions-item>
            <el-descriptions-item label="申请原因" :span="5">{{ form.cause }}</el-descriptions-item>
            <el-descriptions-item label="图片" :span="5" v-if="fileS != null">
              <el-image v-for="(item, index) in fileS" :key="index" :src="imgUrl(item.path)" fit="cover"
                style="width: 23%; height: 200px;  margin-left:1%;margin-right: 1%;"
                :preview-src-list="[imgUrl(item.path)]" disabled />
            </el-descriptions-item>
            <el-descriptions-item label="图片" :span="5" v-else>
              无图片
            </el-descriptions-item>
            <el-descriptions-item label="审批意见" :span="5">{{ form.reason }}</el-descriptions-item>

          </el-descriptions>
        </div>
        <div v-else>
          <el-descriptions title="" direction="vertical" :column="5" border style="margin-bottom:20px;">
            <el-descriptions-item label="学生姓名" :span="2">{{ form.stuName }}</el-descriptions-item>
            <el-descriptions-item label="学号" :span="1">{{ form.sno }}</el-descriptions-item>
            <el-descriptions-item label="假期类别">
              <el-tag v-if="form.parentLevelId != null" type="warning">延假</el-tag>
              <el-tag v-else>请假</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="请假类型">
              <dict-tag :options="dict.type.leave_types" :value="form.leaveType" />
            </el-descriptions-item>
            <el-descriptions-item label="开始时间" :span="2">{{ form.beginTime }}</el-descriptions-item>
            <el-descriptions-item label="结束时间" :span="1">{{ form.endTime }}</el-descriptions-item>
            <el-descriptions-item label="累计天数" :span="1">{{ form.duration }}天</el-descriptions-item>
            <el-descriptions-item label="审核状态">
              <dict-tag :options="dict.type.leave_audit" :value="form.newStatus" />
            </el-descriptions-item>
            <el-descriptions-item label="申请原因" :span="5">{{ form.cause }}</el-descriptions-item>
            <el-descriptions-item label="图片" :span="5" v-if="fileS != null">
              <el-image v-for="(item, index) in fileS" :key="index" :src="imgUrl(item.path)" fit="cover"
                style="width: 23%; height: 200px;  margin-left:1%;margin-right: 1%;"
                :preview-src-list="[imgUrl(item.path)]" disabled />
            </el-descriptions-item>
            <el-descriptions-item label="图片" :span="5" v-else>
              无图片
            </el-descriptions-item>
            <el-descriptions-item label="审批意见" :span="5">{{ form.reason }}</el-descriptions-item>
          </el-descriptions>

          <h3>历史申请</h3>
          <el-tabs tab-position="left">
            <el-tab-pane v-for="(item, index) in oldLeaveInfo" :key="index"
              :label="parseTime(item.beginTime, '{y}-{m}-{d}')">
              <el-descriptions title="" direction="horizontal" :column="4" border>
                <el-descriptions-item label="假期类别" :span="4">
                  <el-tag v-if="item.parentLevelId != null" type="warning"> 延假 </el-tag>
                  <el-tag v-else>请假</el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="请假类型" :span="4">
                  <dict-tag :options="dict.type.leave_types" :value="item.leaveType" />
                </el-descriptions-item>
                <el-descriptions-item label="审核状态" :span="2">
                  <dict-tag :options="dict.type.leave_audit" :value="item.status" />
                </el-descriptions-item>
                <el-descriptions-item label="假期状态" :span="2">
                  <dict-tag :options="dict.type.vacation_status" :value="item.vacationStatus"
                    v-if="item.vacationStatus != null" />
                  <el-tag v-else>审核中</el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="开始时间" :span="2">{{ item.beginTime }}</el-descriptions-item>
                <el-descriptions-item label="结束时间" :span="2">{{ item.endTime }}</el-descriptions-item>
                <el-descriptions-item label="累计天数" :span="4">{{ item.duration / 10 }}天</el-descriptions-item>

                <el-descriptions-item label="申请原因" :span="4">{{ item.cause }}</el-descriptions-item>
              </el-descriptions>

            </el-tab-pane>
          </el-tabs>
        </div>
        <!-- 历史审核 -->
        <div v-if="auditInfo.length != 0">
          <h2>审核详情</h2>
          <p style="padding: 5px">
            <el-steps :active="this.auditInfo.length" align-center >
                <el-step  v-for="(item, index) in auditInfo" :key="index" :title="item.status != null?item.status == 2?'审核通过':'审核不通过':'等待审核'" :status="item.status != null?item.status == 2?'success':'error':'primary'">
                    <template slot="description" >
                        <span>
                          {{item.auditName}}
                          <br/>
                          {{item.createTime}}
                        </span>
                    </template>
                </el-step>
            </el-steps>
          </p>
        </div>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listAuditLeave, getAuditLeave, delAuditLeave, addAuditLeave, updateAuditLeave, findListAuditLeave } from "@/api/backend/auditLeave";
import { getListResources } from "@/api/backend/fileResources";
import { listOldLeave } from "@/api/backend/leave";

export default {
  name: "AuditLeave",
  dicts: ['leave_types', 'leave_audit','vacation_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假审核表格数据
      auditLeaveList: [],
      oldLeaveInfo:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      xqOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuName: null,
      },
      fileS: null,
      // 表单参数
      form: {},
      auditInfo: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    imgUrl(path) {
      return process.env.VUE_APP_BASE_API + path;
    },
    /** 查询请假审核列表 */
    getList() {
      this.loading = true;
      listAuditLeave(this.queryParams).then(response => {
        this.auditLeaveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**  获取图片信息  */
    getImgInfos(fileIds) {
      if (fileIds != null && fileIds != "" && fileIds != "undefined") {
        var Info = {
          fileIds: fileIds
        }
        getListResources(Info).then(response => {
          this.fileS = response.rows;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyLeaveId: null,
        audit: null,
        auditName: null,
        reason: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    handleXq(row) {
      this.reset();
      this.fileS = null;
      this.getImgInfos(row.fileIds);
      this.form = JSON.parse(JSON.stringify(row));
      let data = {
        applyLeaveId: row.applyLeaveId
      }
      findListAuditLeave(data).then(response => {
        if (this.form.parentLevelId != null) {
          var data = {
            Id: this.form.parentLevelId
          }
          listOldLeave(data).then(response => {
            this.oldLeaveInfo = response.rows;
            this.form.beginTime = response.rows[0].beginTime;
            let time = new Date(this.form.endTime).getTime() - new Date(this.form.beginTime).getTime();
            let day = Math.floor(time / (24 * 60 * 60 * 1000)); // 计算天数
            let hour = Math.floor((time % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));// 计算小时数
            if (hour == 0) {
              this.form.duration = day;
            } else if (hour <= 12) {
              this.form.duration = day + 0.5;
            } else {
              this.form.duration = day + 1;
            }
          });
        }
        this.auditInfo = response.rows;

        if(row.newStatus == 1 && row.auditLevel < row.auditNewLevel){
          var pushdata = {
            auditName:"等待下一位教师进行审核",
            createTime:"待审核",
            status:null
          };
          this.auditInfo.push(pushdata)
        }
        this.title = "审核详情";
        this.xqOpen = true;
      });
    },
    xqClose() {
      this.xqOpen = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAuditLeave(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改请假审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuditLeave(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuditLeave(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除请假审核编号为"' + ids + '"的数据项？').then(function () {
        return delAuditLeave(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('backend/auditLeave/export', {
        ...this.queryParams
      }, `auditLeave_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
